﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Incendios0_1.sms;

namespace Incendios0_1.user_managment
{
    /// <summary>
    /// Interaction logic for lookfor_user.xaml
    /// </summary>
    public partial class lookfor_user : Window
    {
        Users user;
        Contacts contact;
        Boolean cancel;

        public lookfor_user()
        {
            InitializeComponent();
            user = null;
            contact = null;
            cancel = true;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            searchUser();
        }

        private void searchUser()
        {
            Users user = UserControler.SearchUser(textBox1.Text);
            if (user == null)
            {
                Microsoft.Windows.Controls.MessageBox.Show("El usuario no existe", "No encontrado", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
            else
            {
                this.user = user;
                Contacts contact = ContactControler.SearchContactByUser(user.UserId);
                this.contact = contact;
                cancel = false;
                this.Close();
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        internal Tuple<Users, Contacts> Search()
        {
            this.ShowDialog();
            if (user !=null)
                return new Tuple<Users, Contacts>(user, contact);
            return null;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (cancel)
                user = null;
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                searchUser();
        }
    }
}
